package com.wxx.design.hungrysingleton;

import java.io.Serializable;

/**
 * @author Jetwang
 * @version 1.0
 * @date 2022/4/11 19:58
 * HungarySingleton 饿汉式单例 线程安全
 */
public class HungarySingleton implements Serializable {
    /**
     * 构造器私有
     */
    private HungarySingleton() {
        // 防止反射破坏
        if (SINGLETON != null) {
            throw new RuntimeException("单例构造器禁止反射调用");
        }
    }

    private final static HungarySingleton SINGLETON;

    static {
        SINGLETON = new HungarySingleton();
    }

    public static HungarySingleton getInstance() {
        return SINGLETON;
    }

    /**
     * 解决序列化破坏问题
     * @return
     */
    public Object readResolve() {
        return SINGLETON;
    }
}
